document.addEventListener('DOMContentLoaded', function() {
    // 获取表单和切换链接元素
    const loginForm = document.getElementById('loginForm');
    const registerForm = document.getElementById('registerForm');
    const showRegister = document.getElementById('showRegister');
    const showLogin = document.getElementById('showLogin');

    // 切换显示注册表单
    showRegister.addEventListener('click', function(e) {
        e.preventDefault();
        loginForm.classList.add('hidden');
        registerForm.classList.remove('hidden');
    });

    // 切换显示登录表单
    showLogin.addEventListener('click', function(e) {
        e.preventDefault();
        registerForm.classList.add('hidden');
        loginForm.classList.remove('hidden');
    });

    // 处理注册表单提交
    registerForm.addEventListener('submit', async function(e) {
        e.preventDefault();
        const username = document.getElementById('regUsername').value;
        const password = document.getElementById('regPassword').value;
        const confirmPassword = document.getElementById('confirmPassword').value;

        // 验证密码
        if (password !== confirmPassword) {
            alert('两次输入的密码不一致！');
            return;
        }

        try {
            const response = await fetch('http://localhost:5000/api/auth/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                credentials: 'include',  // 添加这行
                body: JSON.stringify({ username, password })
            });

            const data = await response.json();
            if (data.success) {
                alert('注册成功！');
                // 注册成功后切换到登录表单
                registerForm.classList.add('hidden');
                loginForm.classList.remove('hidden');
            } else {
                alert(data.message || '注册失败，请重试');
            }
        } catch (error) {
            console.error('注册请求出错:', error);
            alert('注册失败，请重试');
        }
    });

    loginForm.addEventListener('submit', async function(e) {
    e.preventDefault();
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;

    try {
        const response = await fetch('http://localhost:5000/api/auth/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            credentials: 'include',  // 添加这行
            body: JSON.stringify({
                username: username,
                password: password
            })
        });

        const data = await response.json();
        if (data.success) {
            // 存储token和用户名
            localStorage.setItem('token', data.token);
            localStorage.setItem('username', data.username);

            // 确保数据保存后再跳转
            window.location.replace('index.html');
        } else {
            alert(data.message || '登录失败，请检查用户名和密码');
        }
            } catch (error) {
                console.error('Error:', error);
                alert('登录请求失败');
            }
}); // 闭合 loginForm 事件监听器

}); // 闭合 DOMContentLoaded 事件监听器